/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import common.Constant;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import model.OrderDetailsModel;
import util.DataUtil;

/**
 *
 * @author ThanhToan
 */
public class OrderDetailDAO {
    public static boolean insertOrderDetails(OrderDetailsModel obj){
         boolean result = false;
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_insertOrderDetail(?,?,?,?,?)}");
            csmt.setString("orderID", obj.getOrderID());
            csmt.setString("productID", obj.getProductID());
            csmt.setString("number", obj.getNumber());
            csmt.setString("price", obj.getPrice());
            csmt.setString("value", obj.getValue());
            result = DataUtil.executeNonStore(csmt);
         } catch (Exception e) {
             e.printStackTrace();
             result = false;
         }
         return result;
     }

    public static boolean updateOrderDetails(OrderDetailsModel obj){
         boolean result = false;
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_updateOrderDetail(?,?,?,?,?)}");
            csmt.setString("orderID", obj.getOrderID());
            csmt.setString("productID", obj.getProductID());
            csmt.setString("number", obj.getNumber());
            csmt.setString("price", obj.getPrice());
            csmt.setString("value", obj.getValue());
            result = DataUtil.executeNonStore(csmt);
         } catch (Exception e) {
             e.printStackTrace();
             result = false;
         }
         return result;
     }

    public static boolean deleteOrderDetailsByID(String id){
         boolean result = false;
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_deleteOrderDetailByID(?)}");
            csmt.setString("id", id);
            result = DataUtil.executeNonStore(csmt);
         } catch (Exception e) {
             e.printStackTrace();
             result = false;
         }
         return result;
     }

    public static ArrayList<OrderDetailsModel> getOrderDetailsByOrderID(String orderID){
         ArrayList<OrderDetailsModel> orderDetailsList = new ArrayList<OrderDetailsModel>();
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetAllOrderDetailsByOrderID(?)}");
            csmt.setString("orderID", orderID);
            ResultSet rs = DataUtil.executeStore(csmt);
            while(rs.next()){
                OrderDetailsModel orderDetailsModel = new OrderDetailsModel();
                orderDetailsModel.setId(rs.getString("ID"));
                orderDetailsModel.setOrderID(rs.getString("OrderID"));
                orderDetailsModel.setProductID(rs.getString("ProductID"));
                orderDetailsModel.setNumber(rs.getString("Number"));
                orderDetailsModel.setPrice(rs.getString("Price"));
                orderDetailsModel.setProductName(rs.getString("ProductName"));
                orderDetailsModel.setDiscount(rs.getString("Discount"));
                orderDetailsModel.setValue(rs.getString("Value"));
                orderDetailsList.add(orderDetailsModel);
            }
            Constant.NUM_ROWS = orderDetailsList.size();
         } catch (Exception e) {
             e.printStackTrace();
         }
         return orderDetailsList;
     }
}
